package com.jinzecloud.reportapproval.execption;

import com.jinzecloud.reportapproval.utils.R;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestControllerAdvice(basePackages = "com.jinzecloud.fiction.controller")
public class ExceptionControllerAdvice {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R handleException(MethodArgumentNotValidException e){
        Map<String,String> map = null;
        e.printStackTrace();
        log.error("数据校验异常{},异常类型{}",e.getMessage(),e.getClass());
        BindingResult bindingResult = e.getBindingResult();
        if (bindingResult.hasErrors()){
            map = new HashMap<>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String defaultMessage = fieldError.getDefaultMessage();
                String field = fieldError.getField();
                map.put(field,defaultMessage);
            }
        }
        return R.error(Code.VAILD_EXCEPTION.getCode(),Code.VAILD_EXCEPTION.getMsg()).put("data",map);
    }

    @ExceptionHandler(value = IllegalStateException.class)
    public R handleException(IllegalStateException e){
//        Map<String,String> map = null;
        e.printStackTrace();
        log.error("数据校验异常{},异常类型{}",e.getMessage(),e.getClass());
        return R.error(Code.ILLEGALSTATE_EXCEPTION.getCode(),Code.ILLEGALSTATE_EXCEPTION.getMsg());
    }

    @ExceptionHandler(value = NullPointerException.class)
    public R handleException(NullPointerException e){
        Map<String,String> map = null;
        e.printStackTrace();
        log.error("数据校验异常{},异常类型{}",e.getMessage(),e.getClass());
        return R.error(Code.NullPOINTER_EXCEPTION.getCode(),Code.NullPOINTER_EXCEPTION.getMsg()).put("data",map);
    }

    @ExceptionHandler(value = CommunicationsException.class)
    public R handleException(CommunicationsException e){
        Map<String,String> map = null;
        e.printStackTrace();
        log.error("数据校验异常{},异常类型{}",e.getMessage(),e.getClass());
        return R.error(Code.DATACONNECT_EXCEPTION.getCode(),Code.DATACONNECT_EXCEPTION.getMsg()).put("data",map);
    }

    @ExceptionHandler(value = RedisConnectionFailureException.class)
    public R handleException(RedisConnectionFailureException e){
        Map<String,String> map = null;
        e.printStackTrace();
        return R.error(Code.CONNECT_EXCEPTION.getCode(),Code.CONNECT_EXCEPTION.getMsg()).put("data",map);
    }

    @ExceptionHandler(value = RuntimeException.class)
    public R handleException(RuntimeException e){
        Map<String,String> map = null;
        e.printStackTrace();
        return R.error(Code.CONNECT_EXCEPTION.getCode(),Code.CONNECT_EXCEPTION.getMsg()).put("data",map);
    }

    @ExceptionHandler(value = Exception.class)
    public R handleException(Exception e){
        e.printStackTrace();
        Map<String,String> map = null;
        log.error("",e.getMessage(),e.getClass());
        return R.error(Code.UNKNOW_EXCEPTION.getCode(),Code.UNKNOW_EXCEPTION.getMsg()).put("data",map);
    }

}
